home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / TableRow.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  11.5 KB  |  538 lines

  1. package horst;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Rectangle;
  6. import java.awt.Shape;
  7. import java.util.Vector;
  8.  
  9. public class TableRow extends View {
  10.    int[] m_colWidths;
  11.    CellBorder[] m_cellBorders;
  12.    Color m_bgColor;
  13.    int m_valign;
  14.    int m_prefWidth = -1;
  15.    int m_prefHeight = -1;
  16.    int m_minWidth = -1;
  17.    int m_minHeight = -1;
  18.    TableView m_tableView;
  19.  
  20.    public TableRow(View parent, Element e, HTMLPane container) {
  21.       super(parent, e, container);
  22.    }
  23.  
  24.    protected void computeCellBorders() {
  25.       this.m_cellBorders = new CellBorder[super.m_children.length];
  26.       int cellSpacing = this.m_tableView.m_cellSpacing;
  27.  
  28.       for(int i = 0; i < super.m_children.length; ++i) {
  29.          CellBorder r = new CellBorder(this);
  30.          TableCell cell = (TableCell)super.m_children[i];
  31.          Rectangle bounds = cell.getBounds();
  32.          ((Rectangle)r).setBounds(bounds);
  33.          this.m_cellBorders[i] = r;
  34.          this.m_cellBorders[i].bPaint = false;
  35.          View[] children = cell.m_children;
  36.  
  37.          for(int h = 0; h < children.length; ++h) {
  38.             if (children[h].isDisplayableView()) {
  39.                this.m_cellBorders[i].bPaint = true;
  40.                break;
  41.             }
  42.          }
  43.       }
  44.  
  45.    }
  46.  
  47.    protected View elementToView(Element e) {
  48.       if (super.m_elem == e) {
  49.          return this;
  50.       } else {
  51.          for(int i = 0; i < super.m_children.length; ++i) {
  52.             View v = super.m_children[i].elementToView(e);
  53.             if (v != null) {
  54.                return v;
  55.             }
  56.          }
  57.  
  58.          return null;
  59.       }
  60.    }
  61.  
  62.    protected int getBiggestRowSpan() {
  63.       int result = 1;
  64.  
  65.       for(int i = 0; i < super.m_children.length; ++i) {
  66.          TableCell cell = (TableCell)super.m_children[i];
  67.          result = Math.max(result, cell.m_rowSpan);
  68.       }
  69.  
  70.       return result;
  71.    }
  72.  
  73.    protected TableCell getCell(int i) {
  74.       return super.m_children != null && i < super.m_children.length ? (TableCell)super.m_children[i] : null;
  75.    }
  76.  
  77.    protected int getHeightSpec() {
  78.       int heightSpec = -1;
  79.  
  80.       for(int i = 0; i < super.m_children.length; ++i) {
  81.          TableCell cell = (TableCell)super.m_children[i];
  82.          if (cell.m_height >= 0) {
  83.             heightSpec = Math.max(heightSpec, cell.m_height);
  84.          }
  85.       }
  86.  
  87.       return heightSpec;
  88.    }
  89.  
  90.    protected int getMinimumSpan(int axis) {
  91.       if (axis != 1) {
  92.          return 0;
  93.       } else if (this.m_minWidth != -1) {
  94.          return this.m_minWidth;
  95.       } else {
  96.          int span = 0;
  97.  
  98.          for(int i = 0; i < super.m_children.length; ++i) {
  99.             span += super.m_children[i].getMinimumSpan(1);
  100.          }
  101.  
  102.          span += (super.m_children.length - 1) * this.m_tableView.m_cellSpacing;
  103.          this.m_minWidth = span;
  104.          return this.m_minWidth;
  105.       }
  106.    }
  107.  
  108.    protected int getPreferredSpan(int axis) {
  109.       if (axis != 1) {
  110.          return 0;
  111.       } else if (this.m_prefWidth != -1) {
  112.          return this.m_prefWidth;
  113.       } else {
  114.          int m_prefWidth = 0;
  115.  
  116.          for(int i = 0; i < super.m_children.length; ++i) {
  117.             m_prefWidth += super.m_children[i].getPreferredSpan(1);
  118.          }
  119.  
  120.          m_prefWidth += (super.m_children.length - 1) * this.m_tableView.m_cellSpacing;
  121.          return m_prefWidth;
  122.       }
  123.    }
  124.  
  125.    protected TableCell getTableColumn(int col) {
  126.       TableCell tableCell = null;
  127.  
  128.       for(int i = 0; i < super.m_children.length; ++i) {
  129.          TableCell cell = (TableCell)super.m_children[i];
  130.          if (cell != null) {
  131.             if (cell.m_startColumn == col) {
  132.                if (cell.m_endColumn == cell.m_startColumn) {
  133.                   tableCell = cell;
  134.                }
  135.                break;
  136.             }
  137.  
  138.             if (cell.m_endColumn >= col) {
  139.                break;
  140.             }
  141.          }
  142.       }
  143.  
  144.       return tableCell;
  145.    }
  146.  
  147.    protected Rectangle increaseHeight(int increaseY) {
  148.       int rowHeight = 0;
  149.  
  150.       for(int i = 0; i < super.m_children.length; ++i) {
  151.          TableCell cell = (TableCell)super.m_children[i];
  152.          if (cell.m_rowSpan == 1) {
  153.             cell.increaseHeight(increaseY);
  154.             rowHeight = Math.max(rowHeight, cell.getBounds().height);
  155.          }
  156.       }
  157.  
  158.       super.m_bounds.height = rowHeight;
  159.       return super.m_bounds;
  160.    }
  161.  
  162.    protected void init() {
  163.       this.m_tableView = (TableView)super.m_parent;
  164.       this.m_bgColor = Utilities.setColorProperty(this.m_tableView.m_bgColor, "bgcolor", super.m_elem.getAttributes());
  165.       super.m_alignment = Utilities.setAlignmentProperty(false, 0, "align", super.m_elem.getAttributes());
  166.       this.m_valign = Utilities.setAlignmentProperty(true, 1, "valign", super.m_elem.getAttributes());
  167.    }
  168.  
  169.    protected int[] layoutMultiRowCells(int x, int y, int width, int[] rowHeights, LayoutInfo info) {
  170.       int cellSpacing = this.m_tableView.m_cellSpacing;
  171.       int rowHeight = 0;
  172.  
  173.       for(int i = 0; i < super.m_children.length; ++i) {
  174.          TableCell cell = (TableCell)super.m_children[i];
  175.          if (cell.m_rowSpan >= 2) {
  176.             int colspan = cell.m_colSpan;
  177.             int startCol = cell.m_startColumn;
  178.             int endCol = cell.m_endColumn;
  179.             int xPos = x;
  180.  
  181.             for(int j = 0; j < startCol; ++j) {
  182.                xPos += this.m_colWidths[j] + cellSpacing;
  183.             }
  184.  
  185.             int layoutWidth = 0;
  186.  
  187.             for(int j = startCol; j <= endCol; ++j) {
  188.                layoutWidth += this.m_colWidths[j];
  189.             }
  190.  
  191.             if (colspan > 1) {
  192.                layoutWidth += (colspan - 1) * cellSpacing;
  193.             }
  194.  
  195.             LayoutInfo subInfo = new LayoutInfo();
  196.             subInfo.bPaginate = info.bPaginate;
  197.             subInfo.pageBreak = info.pageBreak;
  198.             Rectangle bounds = cell.layout(xPos, y, layoutWidth, subInfo);
  199.             int startRow = cell.m_startRow;
  200.             int endRow = cell.m_endRow;
  201.             int span = 0;
  202.  
  203.             for(int j = startRow; j <= endRow; ++j) {
  204.                span += rowHeights[j];
  205.             }
  206.  
  207.             if (cell.m_rowSpan > 1) {
  208.                int nDefinedRows = this.m_tableView.m_nDefinedRows;
  209.                if (startRow < nDefinedRows - 1 && endRow >= nDefinedRows - 1) {
  210.                   int nValidRowsSpanned = nDefinedRows - startRow;
  211.                   if (nValidRowsSpanned > 0 && cellSpacing > 0) {
  212.                      span += (nValidRowsSpanned - 1) * cellSpacing;
  213.                   }
  214.                } else if (cellSpacing > 0) {
  215.                   span += (cell.m_rowSpan - 1) * cellSpacing;
  216.                }
  217.             }
  218.  
  219.             if (span < bounds.height) {
  220.                int diff = bounds.height - span;
  221.                boolean bCheckSpecs = true;
  222.                int[] heightSpecs = this.m_tableView.m_rowHeightSpecs;
  223.  
  224.                while(diff > 0) {
  225.                   for(int row = startRow; row <= endRow && diff > 0; ++row) {
  226.                      if (!bCheckSpecs || heightSpecs[row] < 0 || heightSpecs[row] > 0 && heightSpecs[row] > rowHeights[row]) {
  227.                         this.m_tableView.increaseRowHeight(row, 1);
  228.                         int var10002 = rowHeights[row]++;
  229.                         --diff;
  230.                      }
  231.                   }
  232.  
  233.                   bCheckSpecs = false;
  234.  
  235.                   for(int row = startRow; row <= endRow && diff > 0; ++row) {
  236.                      if (heightSpecs[row] < 0 || heightSpecs[row] > 0 && heightSpecs[row] > rowHeights[row]) {
  237.                         bCheckSpecs = true;
  238.                         break;
  239.                      }
  240.                   }
  241.                }
  242.             }
  243.          }
  244.       }
  245.  
  246.       int h = 0;
  247.       View[] vws = this.m_tableView.m_children;
  248.  
  249.       for(int i = 0; i < vws.length; ++i) {
  250.          TableRow r = (TableRow)vws[i];
  251.          if (r == this) {
  252.             h = rowHeights[i];
  253.             break;
  254.          }
  255.       }
  256.  
  257.       super.m_bounds.height = h;
  258.       int w = 0;
  259.  
  260.       for(int i = 0; i < this.m_colWidths.length; ++i) {
  261.          w += this.m_colWidths[i];
  262.          if (i != this.m_colWidths.length - 1) {
  263.             w += cellSpacing;
  264.          }
  265.       }
  266.  
  267.       super.m_bounds.width = w;
  268.       return rowHeights;
  269.    }
  270.  
  271.    protected Rectangle layoutSingleRowCells(int x, int y, int width, LayoutInfo info) {
  272.       super.m_bounds = new Rectangle(x, y, 0, 0);
  273.       int cellSpacing = this.m_tableView.m_cellSpacing;
  274.       int rowWidth = 0;
  275.       int rowHeight = 0;
  276.       int nColumns = this.m_tableView.m_nColumns;
  277.  
  278.       for(int i = 0; i < super.m_children.length; ++i) {
  279.          TableCell cell = (TableCell)super.m_children[i];
  280.          if (cell.m_rowSpan <= 1) {
  281.             int colspan = cell.m_colSpan;
  282.             int startCol = cell.m_startColumn;
  283.             int endCol = cell.m_endColumn;
  284.             int xPos = x;
  285.  
  286.             for(int j = 0; j < startCol; ++j) {
  287.                xPos += this.m_colWidths[j] + cellSpacing;
  288.             }
  289.  
  290.             int layoutWidth = 0;
  291.  
  292.             for(int j = startCol; j <= endCol; ++j) {
  293.                layoutWidth += this.m_colWidths[j];
  294.             }
  295.  
  296.             if (colspan > 1) {
  297.                layoutWidth += (colspan - 1) * cellSpacing;
  298.             }
  299.  
  300.             LayoutInfo subInfo = new LayoutInfo();
  301.             subInfo.bPaginate = false;
  302.             Rectangle bounds = cell.layout(xPos, y, layoutWidth, subInfo);
  303.             rowWidth += bounds.width;
  304.             if (endCol != nColumns - 1) {
  305.                rowWidth += cellSpacing;
  306.             }
  307.  
  308.             Math.max(layoutWidth, bounds.width);
  309.             if (cell.m_rowSpan == 1) {
  310.                rowHeight = Math.max(rowHeight, bounds.height);
  311.             }
  312.          }
  313.       }
  314.  
  315.       super.m_bounds = new Rectangle(x, y, rowWidth, rowHeight);
  316.       return super.m_bounds;
  317.    }
  318.  
  319.    protected void makeChildren(ViewFactory factory) {
  320.       Element elem = super.m_elem;
  321.       int nCount = elem.getElementCount();
  322.       Vector temp = new Vector();
  323.       int tableColumn = 0;
  324.  
  325.       for(int i = 0; i < nCount; ++i) {
  326.          Element childElement = elem.getElementAt(i);
  327.          int type = childElement.getType();
  328.          if (type == 5 || type == 4) {
  329.             TableCell cell = new TableCell(this, this.m_tableView, childElement, super.m_container);
  330.             cell.setTableColumns(tableColumn);
  331.             tableColumn += cell.m_colSpan;
  332.             temp.addElement(cell);
  333.          }
  334.       }
  335.  
  336.       super.m_children = new View[temp.size()];
  337.       temp.copyInto(super.m_children);
  338.  
  339.       for(int i = 0; i < super.m_children.length; ++i) {
  340.          super.m_children[i].makeChildren(factory);
  341.       }
  342.  
  343.    }
  344.  
  345.    protected View modelToView(int pos) {
  346.       if (pos >= super.m_elem.m_p0 && pos <= super.m_elem.m_p1) {
  347.          return this;
  348.       } else {
  349.          for(int i = 0; i < super.m_children.length; ++i) {
  350.             View v = super.m_children[i].modelToView(pos);
  351.             if (v != null) {
  352.                return v;
  353.             }
  354.          }
  355.  
  356.          return null;
  357.       }
  358.    }
  359.  
  360.    protected void move(int x, int y, boolean bMoveFloaters) {
  361.       for(int i = 0; i < super.m_children.length; ++i) {
  362.          super.m_children[i].move(x, y, bMoveFloaters);
  363.       }
  364.  
  365.       if (this.m_cellBorders != null) {
  366.          for(int i = 0; i < this.m_cellBorders.length; ++i) {
  367.             CellBorder var10000 = this.m_cellBorders[i];
  368.             var10000.x += x;
  369.             var10000 = this.m_cellBorders[i];
  370.             var10000.y += y;
  371.          }
  372.       }
  373.  
  374.       Rectangle var7 = super.m_bounds;
  375.       var7.x += x;
  376.       var7 = super.m_bounds;
  377.       var7.y += y;
  378.    }
  379.  
  380.    protected boolean occupies(int row, int col) {
  381.       for(int i = 0; i < super.m_children.length; ++i) {
  382.          TableCell cell = (TableCell)super.m_children[i];
  383.          if (cell.occupies(row, col)) {
  384.             return true;
  385.          }
  386.       }
  387.  
  388.       return false;
  389.    }
  390.  
  391.    protected int pageBreakAdjust(LayoutInfo info) {
  392.       Rectangle origBounds = new Rectangle(super.m_bounds);
  393.       int maxCellHeight = 0;
  394.  
  395.       for(int i = 0; i < super.m_children.length; ++i) {
  396.          TableCell cell = (TableCell)super.m_children[i];
  397.          cell.pageBreakAdjust(info);
  398.          maxCellHeight = Math.max(maxCellHeight, cell.m_cellBounds.height);
  399.       }
  400.  
  401.       for(int i = 0; i < super.m_children.length; ++i) {
  402.          TableCell cell = (TableCell)super.m_children[i];
  403.          if (maxCellHeight > cell.m_cellBounds.height) {
  404.             cell.increaseHeight(maxCellHeight - cell.m_cellBounds.height);
  405.          }
  406.       }
  407.  
  408.       super.m_bounds.height = maxCellHeight;
  409.       this.computeCellBorders();
  410.       return super.m_bounds.height - origBounds.height;
  411.    }
  412.  
  413.    public void paint(Graphics g, Shape alloc) {
  414.       for(int i = 0; i < super.m_children.length; ++i) {
  415.          super.m_children[i].paint(g, alloc);
  416.       }
  417.  
  418.       this.paintCellBorders(g);
  419.    }
  420.  
  421.    protected void paintCellBorders(Graphics g) {
  422.       if (this.m_cellBorders != null && this.m_tableView.m_borderSize > 0) {
  423.          Color oldColor = g.getColor();
  424.  
  425.          for(int i = 0; i < this.m_cellBorders.length; ++i) {
  426.             CellBorder r = this.m_cellBorders[i];
  427.             if (r != null && r.bPaint) {
  428.                g.setColor(Utilities.getDarkBorderColor());
  429.                g.drawLine(r.x, r.y, r.x + r.width - 1, r.y);
  430.                g.drawLine(r.x, r.y, r.x, r.y + r.height - 1);
  431.                g.setColor(Utilities.getBrightBorderColor());
  432.                g.drawLine(r.x, r.y + r.height - 1, r.x + r.width - 1, r.y + r.height - 1);
  433.                g.drawLine(r.x + r.width - 1, r.y, r.x + r.width - 1, r.y + r.height - 1);
  434.             }
  435.          }
  436.  
  437.          g.setColor(oldColor);
  438.       }
  439.  
  440.    }
  441.  
  442.    protected void paintFocusBox(Graphics g, Shape alloc) {
  443.       for(int i = 0; i < super.m_children.length; ++i) {
  444.          super.m_children[i].paintFocusBox(g, alloc);
  445.       }
  446.  
  447.    }
  448.  
  449.    protected int[] setCellHeights(int[] rowHeights, LayoutInfo info) {
  450.       for(int i = 0; i < super.m_children.length; ++i) {
  451.          TableCell cell = (TableCell)super.m_children[i];
  452.          int cellHeight = cell.getBounds().height;
  453.          int rowspan = cell.m_rowSpan;
  454.          if (rowspan <= 1) {
  455.             int rowHeight = rowHeights[cell.m_startRow];
  456.             if (rowHeight > cellHeight) {
  457.                cell.increaseHeight(rowHeight - cellHeight);
  458.             }
  459.          } else {
  460.             int targetHeight = 0;
  461.  
  462.             for(int j = cell.m_startRow; j <= cell.m_endRow; ++j) {
  463.                targetHeight += rowHeights[j];
  464.             }
  465.  
  466.             int cellSpacing = this.m_tableView.m_cellSpacing;
  467.             int startRow = cell.m_startRow;
  468.             int endRow = cell.m_endRow;
  469.             int nDefinedRows = this.m_tableView.m_nDefinedRows;
  470.             if (startRow < nDefinedRows - 1 && endRow >= nDefinedRows - 1) {
  471.                int nValidRowsSpanned = nDefinedRows - startRow;
  472.                if (nValidRowsSpanned > 0 && cellSpacing > 0) {
  473.                   targetHeight += (nValidRowsSpanned - 1) * cellSpacing;
  474.                }
  475.             } else if (cellSpacing > 0) {
  476.                targetHeight += (cell.m_rowSpan - 1) * cellSpacing;
  477.             }
  478.  
  479.             if (targetHeight > cellHeight) {
  480.                cell.increaseHeight(targetHeight - cellHeight);
  481.             }
  482.          }
  483.       }
  484.  
  485.       int[] rowHeightAdjustments = new int[rowHeights.length];
  486.       int maxCellHeight = 0;
  487.  
  488.       for(int i = 0; i < super.m_children.length; ++i) {
  489.          TableCell cell = (TableCell)super.m_children[i];
  490.          int pagingAdjustment = cell.doCellVerticalAlignment(info);
  491.          maxCellHeight = Math.max(maxCellHeight, cell.getBounds().height);
  492.          if (pagingAdjustment > 0) {
  493.             this.m_tableView.increasePreviousCellHeights(this, pagingAdjustment);
  494.             int[] rowHeightValues = new int[rowHeights.length];
  495.             int space = pagingAdjustment;
  496.  
  497.             while(space > 0) {
  498.                for(int j = cell.m_startRow; j <= cell.m_endRow && space > 0; ++j) {
  499.                   int var10002 = rowHeightValues[j]++;
  500.                   --space;
  501.                }
  502.             }
  503.  
  504.             for(int j = cell.m_startRow; j <= cell.m_endRow; ++j) {
  505.                rowHeightAdjustments[j] = Math.max(rowHeightAdjustments[j], rowHeightValues[j]);
  506.             }
  507.          }
  508.       }
  509.  
  510.       for(int row = 0; row < rowHeightAdjustments.length; ++row) {
  511.          if (rowHeightAdjustments[row] > 0) {
  512.             rowHeights[row] += rowHeightAdjustments[row];
  513.             TableRow rv = this.m_tableView.getRow(row + 1);
  514.             if (rv != null) {
  515.                this.m_tableView.moveRows(rowHeightAdjustments[row], rv);
  516.             }
  517.          }
  518.       }
  519.  
  520.       return rowHeights;
  521.    }
  522.  
  523.    protected ElementViewInfo viewToModel(int x, int y) {
  524.       ElementViewInfo info = null;
  525.  
  526.       for(int i = 0; i < super.m_children.length; ++i) {
  527.          if (super.m_children[i].contains(x, y)) {
  528.             info = super.m_children[i].viewToModel(x, y);
  529.             if (info != null) {
  530.                break;
  531.             }
  532.          }
  533.       }
  534.  
  535.       return info;
  536.    }
  537. }
  538.